const express = require('express')
const router = express.Router()
const {register,login, getUsers,editUser,pwdchange} = require('../controller/user')
const writeLog = require('../middleware/writeLog')
const checkToken = require('../middleware/checkToken')
const multer = require('multer')
const path = require('path')
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, path.join(__dirname, '../', 'uploads'))
    },
    filename: function (req, file, cb) {
        let extname = path.extname(file.originalname)
        let time = +new Date()
        let str = 'abcdefghijklmnopqrstuvwxyz0123456789'
        let random = ''
        for(let a=0; a<6;a++) {
            random += str[ Math.floor(Math.random() * str.length) ]
        }
        // cb(null, 文件名称)
        cb(null, time + random + extname)
    }
})
const upload = multer({ storage: storage })

router.use(writeLog)

//注册
router.post('/register', register)
//登录
router.post('/login', login)

//获取用户信息=>需要传入id来验证
router.get('/getUsers/:_id',checkToken, getUsers)
// 修改用户信息
router.put('/editUser/:_id', checkToken, upload.single('avatar'), editUser)

router.put('/pwdchange/:_id',checkToken, pwdchange)

module.exports = router